﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LogLib;
using System.Reflection;
using System.IO;
using System.Threading.Tasks;
using PipeClient;

namespace ScheduleAutoAction
{
    public partial class FormScheduleAction : Form
    {
        #region 专用于异步发送启动消息
        protected IntPtr ParentFormHandle = IntPtr.Zero;
        /// <summary>
        /// 指示是否已经通知主窗体加载完成
        /// </summary>
        protected bool _bNotified = false;
        public string AppName = Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase);
        #endregion

        private ucScheduleCleaning ucCleaning = null;

        private ucScheduleShutdown ucShutdown = null;

        public FormScheduleAction()
        {
            ucCleaning = new ucScheduleCleaning();
            ucShutdown = new ucScheduleShutdown();
            InitializeComponent();
        }

        public FormScheduleAction(string arg)
        {
            if (!string.IsNullOrEmpty(arg))
            {
                ParentFormHandle = (IntPtr)Int32.Parse(arg);
            }
            ucCleaning = new ucScheduleCleaning();
            ucShutdown = new ucScheduleShutdown();
            InitializeComponent();
        }

        private void InitTabChildPages()
        {
            tabPage1.Controls.Add(ucCleaning);
            tabPage2.Controls.Add(ucShutdown);
            ucCleaning.Init();
            ucShutdown.Init();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitTabChildPages();
            if (ParentFormHandle != IntPtr.Zero)
            {
                OnAppStartupFinished();
                this.WindowState = FormWindowState.Minimized;
            }      
        }

        private void 配置任务计划ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormScheduleSettings form = new FormScheduleSettings();
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ucCleaning.StopAndRemoveSchedule();
                ucShutdown.StopScheduleTask();
                ucCleaning.Init();
                ucShutdown.Init();
            }
        }

        private void 终止任务并退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {           
            this.Close();
        }

        private void FormScheduleAction_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (ucCleaning.Option.Enabled)
            {
                ucCleaning.StopAndRemoveSchedule();          
            }
            if (ucShutdown.Option.Enabled)
            {
                ucShutdown.StopScheduleTask();
            }
        }

        private void 显示主界面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

        /// <summary>
        /// 通知主窗体该进程启动已经完成
        /// </summary>
        protected void OnAppStartupFinished(int code = 1, string msg = "程序加载成功")
        {
            // 当父窗体为空时，不需要通知
            if (IntPtr.Zero == ParentFormHandle || _bNotified)
            {
                Log.WriteAsync(string.Format("{0}获取父进程主窗口句柄为空，不启用进程监测.", AppName));
                return;
            }
            string sendMsg = string.Format("{0}#{1}#{2}", AppName, code, msg);
            Pipe t = new Pipe(5, sendMsg, "MainStartup");
            Task.Factory.StartNew(new Action(t.run));
            Log.WriteAsync("已经通知主窗体启动完成: " + AppName);
            _bNotified = true;
        }

    }
}
